package com.wzy.springboot.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.wzy.springboot.dao.SysUserRepository;
import com.wzy.springboot.domain.SysUser;

public class CustomUserService implements UserDetailsService {

	@Autowired
	private SysUserRepository userRepository;

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		SysUser user = userRepository.findByUsername(username);

		if (user == null) {
			throw new UsernameNotFoundException("用户名不存在!!");
		}

		return new SysUser(user.getId(),user.getUsername(), this.passwordEncoder.encode(user.getPassword()),user.getRoles());
	}
}
